﻿using HighThroughput.ShareData;
using NPOI.SS.Formula.Functions;
using OSSUtility;
using OSSUtility.Communicate;
using OSSUtility.Devices;
using OSSUtility.Models;
using Ozone.BLL;
using Ozone.Common;
using Ozone.Help;
using Ozone.Models;
using Ozone.ShareData;
using Ozone.Views.Video;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Management.Instrumentation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using static Ozone.Models.KeyPoint;
using static SharpDX.Utilities;

namespace Ozone.Device
{
    //设备控制中心
    public class DeviceCenter
    {

        static public DeviceCenter g_DeviceCenter { get; set; }

        static public DeviceCenter Entity { get { return g_DeviceCenter; } }

        #region   Radis缓存

        static public RedisKeyWatcher redisKeyWatcher = null;

        #endregion
        //摄像头参数
        public List<HKCam> hKCams = new List<HKCam>();
        public List<string> VideoIPs = new List<string>();
        public List<string> VideoTitles = new List<string>();
        public List<string> VideoPasswords = new List<string>();
        public List<UCVideoMonitor> uCVideoMonitors = new List<UCVideoMonitor>();
        public DeviceCenter()
        {
            bool IsRadisExist = InitConnectRadis();
            if (IsRadisExist)
            {
                LogHelper.Info("Radis状态重置成功!");
            }
            if (!InitCamParam())
            {
                LogHelper.Info("相机参数初始化失败");
                return;
            }
            LogHelper.Info("相机参数初始化成功");
        }
        private bool InitCamParam()
        {
            if(!GetVideoInfo())
                return false;
            hKCams = GetHKCams();
            if ((hKCams.Count<1)||(VideoIPs.Count < hKCams.Count)||(VideoTitles.Count < hKCams.Count)||(VideoPasswords.Count < hKCams.Count))
                return false;
            return true;
        }
        private bool GetVideoInfo()
        {
            VideoIPs = GetInfo("VideoIP");
            VideoTitles = GetInfo("VideoTitle");
            VideoPasswords = GetInfo("VideoPassWord");
            if((VideoIPs == null) || (VideoIPs.Count < 1)|| (VideoTitles == null) || (VideoTitles.Count < 1)|| (VideoPasswords == null) || (VideoPasswords.Count < 1))
            {
                return false;
            }
            return true;
        }
        private List<HKCam> GetHKCams()
        {
            List<HKCam> hKCamsTemp = new List<HKCam>();
            int VideoNum = Utility.str2int(ConfigurationManager.AppSettings["VideoNumber"]);
            if(VideoNum < 1)
                return hKCamsTemp;
            for (int i = 0; i < VideoNum; i++)
            {
                hKCamsTemp.Add(new HKCam());
            }
            return hKCamsTemp;
        } 
        private List<string> GetInfo(string key)
        {
            try
            {
                List<string> lstInfo = new List<string>();
                string info = ConfigurationManager.AppSettings[key];
                info = info.Replace("\r", "");
                info = info.Replace("\n", "");
                info = info.Replace("\t", "");
                info = info.Replace(" ", "");
                string[] arInfo = info.Split('|');
                foreach (string item in arInfo)
                {
                    lstInfo.Add(item);
                }
                return lstInfo;
            }
            catch(Exception e)
            {
                return null;
            }
        }

        private bool InitConnectRadis()
        {
            bool IsRadisExist = false;
            try
            {
                LogHelper.Info("Radis连接中!");
                int ConnectNuber = 0;
                //循环等待链接
                while (true)
                {
                    if (ConnectRadis())
                    {
                        IsRadisExist = true;
                        break;
                    }
                    ConnectNuber++;

                    if (ConnectNuber > 1)
                    {
                        break;
                    }
                    Thread.Sleep(6000);
                }
            }
            catch (Exception)
            {

                IsRadisExist = false;
            }

            return IsRadisExist;
        }

        private bool ConnectRadis()
        {
            bool IsRadisExist = false;
            try
            {
                redisKeyWatcher = new RedisKeyWatcher("127.0.0.1,password=password01!");
                redisKeyWatcher.StartWatching();
                IsRadisExist = RadisService.KeyExists("RadisHeartbeat");
                if (IsRadisExist)
                {
                    LogHelper.Info("Radis连接成功!");
                }
                else
                {
                    LogHelper.Info("Radis连接失败!");
                }
                IsRadisExist = true;
            }
            catch (Exception ex)
            {
                LogHelper.Info("Radis连接失败!" + ex.Message);
            }

            return IsRadisExist;
        }
    }
}
